<template>
  <view class="page login">
    <view class="title">
      <image src="https://hengxiao-resource.oss-cn-hangzhou.aliyuncs.com/web/resources/images/hengxiao/login/title.png" mode="scaleToFill" />
    </view>
    <view class="text"> 让英语学习更简单 </view>
    <view class="content">
      <view class="box">
        <view class="avatar">
          <image src="https://hengxiao-resource.oss-cn-hangzhou.aliyuncs.com/web/resources/images/hengxiao/login/avatar.png" mode="widthFix"></image>
        </view>
        <view class="form">
          <u-form :model="form" :rules="rules" ref="uForm" label-width="50">
            <u-form-item prop="username">
              <u-icon size="36" name="account"></u-icon>
              <u-input
                type="text"
                placeholder="输入账号"
                placeholder-style="color: #fff;"
                v-model="form.username"
              />
            </u-form-item>
            <u-form-item prop="password">
              <u-icon size="36" name="lock"></u-icon>
              <u-input
                type="password"
                placeholder="输入密码"
                v-model="form.password"
                placeholder-style="color: #fff;"
              />
            </u-form-item>
            <u-form-item class="u-form-register">
              <view class="register" @click="register"> 注册 </view>
            </u-form-item>
            <u-button @click="submit" class="u-form-submit">登录</u-button>
          </u-form>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入账号",
            trigger: ["blur"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur"],
          },
        ],
      },
    };
  },
  onLoad() {},
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    submit() {
      this.$refs.uForm.validate((valid) => {
        if (valid) {
          this.$store.dispatch("Login", this.form).then(() => {
            uni.redirectTo({
              url: "/pages/index/index",
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    register() {
      uni.navigateTo({ url: "/pages/register/register" });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./login";
</style>
